<?php
namespace app\reportExport\controller;
use app\common\Base;
use app\common\Excel;
use think\Db;

class HeadMasterMailbox extends Base {
	
	public function index() {
		return $this->fetch();
	}

	public function pageQuery() {
		$page = input('page');
		$rows = input('rows');
		$startDate = input("startDate");
		$endDate = input("endDate");
		$isReply = input("isReply");
		$where = [];
		if($isReply) $where["IS_REPLY"] = array("eq",$isReply);
		if($startDate && $endDate) {
			$where["pm.CREATE_TIME"] = array("between",[$startDate,$endDate]);
		}else {
			if($startDate) $where["pm.CREATE_TIME"] = array("egt", $startDate);
			if($endDate) $where["pm.CREATE_TIME"] = array("elt", $endDate);
		}
		$result = Db::table("t_principal_mailbox pm")
				  ->join("t_parent_info pi","pm.PARENT_ID = pi.ID","LEFT")
				  ->join("t_parent_student_rel pr","pr.PARENT_ID = pi.ID and pm.STUDENT_ID = pr.STUDENT_ID","LEFT")
				  ->join("学生信息 xs","pm.STUDENT_ID = xs.ID","LEFT")
				  ->join("教师信息 t","pm.TEACHER_ID = t.ID","LEFT")
				  ->field("pm.PROBLEM,pm.REPLY,pm.PARENT_ID,pm.TEACHER_NAME,pm.IS_REPLY, pm.CREATE_TIME,pm.REPLY_TIME,pi.NAME PARENT_NAME,xs.姓名 STUDENT_NAME,xs.年级号 GRADE,xs.学籍类型 STUDY_TYPE,xs.班号 CLAZZ")
				  ->where($where)
				  ->page($page, $rows)
				  ->order("CREATE_TIME desc")
				  ->select();
		$data = [];
		foreach ($result as $key => $obj) {
			$isReply = $obj["IS_REPLY"];
			$obj["IS_REPLY"] = $isReply == 0 ? "未答复" : "已答复";
			$obj["GRADE_NAME"] = getGradeName($obj["GRADE"],$obj["STUDY_TYPE"]).$obj["CLAZZ"]."班";
			$data[$key] = $obj;
		}
		$count = Db::table("t_principal_mailbox pm")
				  ->where($where)
				  ->count();
		$grid["rows"] = $data;
		$grid["total"] = $count;
		return $grid;
	}

	public function exportExcel() {
		$startDate = input("startDate");
		$endDate = input("endDate");
		$isReply = input("isReply");
		$where = [];
		if($isReply) $where["IS_REPLY"] = array("eq",$isReply);
		if($startDate && $endDate) {
			$where["pm.CREATE_TIME"] = array("between",[$startDate,$endDate]);
		}else {
			if($startDate) $where["pm.CREATE_TIME"] = array("egt", $startDate);
			if($endDate) $where["pm.CREATE_TIME"] = array("elt", $endDate);
		}
		$result = Db::table("t_principal_mailbox pm")
				  ->join("t_parent_info pi","pm.PARENT_ID = pi.ID","LEFT")
				  ->join("t_parent_student_rel pr","pr.PARENT_ID = pi.ID and pm.STUDENT_ID = pr.STUDENT_ID","LEFT")
				  ->join("学生信息 xs","pm.STUDENT_ID = xs.ID","LEFT")
				  ->field("pm.PROBLEM,pm.REPLY,pm.TEACHER_NAME,pm.IS_REPLY, pm.CREATE_TIME,pm.REPLY_TIME,pi.NAME PARENT_NAME,xs.姓名 STUDENT_NAME,xs.年级号 GRADE,xs.学籍类型 STUDY_TYPE,xs.班号 CLAZZ")
				  ->where($where)
				  ->order("CREATE_TIME desc")
				  ->select();
		$data = [];
		foreach ($result as $key => $obj) {
			$isReply = $obj["IS_REPLY"];
			$obj["IS_REPLY"] = $isReply == 0 ? "未答复" : "已答复";
			$obj["GRADE_NAME"] = getGradeName($obj["GRADE"],$obj["STUDY_TYPE"]).$obj["CLAZZ"]."班";
			$data[$key] = $obj;
		}
		//excel表头内容
		$header = array("班级"=>"GRADE_NAME","学生姓名"=>"STUDENT_NAME","家长昵称"=>"PARENT_NAME","内容"=>"PROBLEM","发送时间"=>"CREATE_TIME","回复状态"=>"IS_REPLY","回复教师"=>"TEACHER_NAME","回复内容"=>"REPLY","回复时间"=>"REPLY_TIME");
		Excel::toExcel($data,'校长信箱',$header);
		exit;
	}

}
